package com.yuwei.module_device.viewmodel

import android.util.Log
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.vm.BaseViewModel2
import com.yuwei.common.entity.DeviceEntity
import com.yuwei.common.entity.DeviceEntityItem
import com.yuwei.module_device.model.DeviceRepository
import kotlinx.coroutines.launch

class DeviceViewModel:BaseViewModel2<DeviceRepository>() {
    //val deviceLiveData=MutableLiveData<DeviceEntity>()
    override fun createRepository(): DeviceRepository = DeviceRepository()
    val getDeviceSuccess by lazy { MutableLiveData<DeviceEntity>() }
    val addDeviceSuccess by lazy { MutableLiveData<DeviceEntityItem>() }
    fun deviceList(){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.getDeviceList()
            Log.d("device_list","getDeviceList: ${result.data}")
            if (result.code==200){
                getDeviceSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取设备列表失败  ${result.message}")
            }
        }
    }
    fun addDevice(deviceEntityItem: DeviceEntityItem){
        mViewModelScope.launch(mDispatcher) {
            val result=mRepository.addDevice(deviceEntityItem)
            Log.d("add_device","addDevice: ${result.data}")
            if (result.code==200){
                addDeviceSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("添加设备失败  ${result.message}")
            }
        }
    }
}